<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>过渡动效</title>
    <script src="https://unpkg.com/vue@2.7.7/dist/vue.js"></script>
    <script src="https://unpkg.com/vue-router@3.6.5/dist/vue-router.js"></script>
    <style type="text/css">
      
      /* --------------------------- */
      .fade-enter-active, .fade-leave-active {
        transition: opacity .5s;
      }
      .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
        opacity: 0;
      }
      /* --------------------------- */
      .bounce-enter-active {
        animation: bounce-in .5s;
      }
      .bounce-leave-active {
        animation: bounce-in .5s reverse;
      }
      @keyframes bounce-in {
        0% {
          transform: scale(0);
        }
        50% {
          transform: scale(1.5);
        }
        100% {
          transform: scale(1);
        }
      }
      /* --------------------------- */
    </style>
  </head>
  <body>
    <!-- 1 单个路由的过渡 -->
    <div id="app1" style="border: 1px dashed red;margin-bottom: 2px;">
      <router-link to="/foo/bar">to /foo/bar</router-link><br />
      <router-link to="/login">to /login</router-link><br />
      <!-- <transition name="fade"> -->
        <router-view></router-view>
      <!-- </transition> -->
    </div>
    <!-- 2 基于路由的动态过渡 -->
    <div id="app2" style="border: 1px dashed blue;margin-bottom: 2px;">
      <router-link to="/depth1">to /depth1</router-link><br />
      <router-link to="/depth1/depth2">to /depth1/depth2</router-link><br />
      <!-- 使用动态的 transition name -->
      <transition :name="transitionName">
        <router-view></router-view>
      </transition>
    </div>
    
    <script type="text/javascript">
      // 1 单个路由的过渡
      const Foo = {
        template: `
          <transition name="fade">
            <div>Foo:<router-view></router-view></div>
          </transition>
        `
      }
      
      const Bar = {template: '<div>Bar</div>'}
      
      const Login = {
        template: `
          <transition name="bounce">
            <div>Login: <br />Username</div>
          </transition>
        `
      }
      
      const router = new VueRouter({
        routes: [
          {
            path: '/foo', 
            component: Foo,
            children: [
              {
                path: 'bar',
                component: Bar,
                meta: {requiresAuth: true}
              }
            ]
          },
          {
            path: '/login',
            component: Login
          }
        ]
      })
      
      var loggedIn = false
      router.beforeEach((to, from, next) => {
        if (to.matched.some(record => record.meta.requiresAuth)) {
          // this route requires auth, check if logged in
          // if not, redirect to login page.
          //   if (!auth.loggedIn()) {
          if (loggedIn) {
            next({
              path: '/login',
              query: {redirect: to.fullPath}
            })
          } else{
            next()
          }
        } else {
          next() // 确保一定要调用 next()
        }
      })
      
      new Vue({
        el: '#app1',
        router
      })

      // 2 基于路由的动态过渡
      const Depth1 = {
        template: '<div>Depth1: <router-view></router-view></div>',
        // 在父组件内 watch $route 决定使用哪种过渡
        watch: {
          '$route' (to, from) {
            const toDepth = to.path.split('/').length
            const fromDepth = from.path.split('/').length
            console.log('toDepth: ' + toDepth)
            console.log('fromDepth: ' + fromDepth)
            // this.transitionName = toDepth < fromDepth ? 'slide-right' : 'slide-left'
            this.transitionName = toDepth < fromDepth ? 'fade' : 'bounce'
          }
        }
      }
      const Depth2 = {template: '<div>Depth2</div>'}
      
      const router2 = new VueRouter({
        routes: [
          {
            path: '/depth1', 
            component: Depth1,
            children: [
              {
                path: 'depth2',
                component: Depth2
              }
            ]
          }
        ]
      })

      new Vue({
        el: '#app2',
        data: {
          transitionName: ''
        },
        router: router2
      })

    </script>
  </body>
</html>